﻿// <copyright file="TTableStorageQueryTest.cs" company="N/A">
//     Copyright (c) Nicholas Barrett and Benjamin Kasten.  All rights reserved.
//
//     This program is free software; you can redistribute it and/or modify it
//     under the terms of the GNU General Public License as published by the
//     Free Software Foundation; either version 2 of the License, or (at your
//     option) any later version.
//
//     This program is distributed in the hope that it will be useful, but
//     WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
//     Public License for more details.
//
//     You should have received a copy of the GNU General Public License along
//     with this program; if not, write to the Free Software Foundation, 
//     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// </copyright>

namespace BetterAzureTableStorage.Tests
{
    using BetterAzureTableStorage;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Linq.Expressions;
    using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;

    /// <summary>
    ///This is a test class for TTableStorageQueryTest and is intended
    ///to contain all TTableStorageQueryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TTableStorageQueryTest
    {
        [TestMethod()]
        public void GetEnumeratorTest()
        {
            bool hasException = false;
            try
            {
                GetEnumeratorTestHelper<GenericParameterHelper>();
            }
            catch (Exception e)
            {
                Assert.AreEqual<Type>(typeof(ArgumentNullException), e.GetType());
                Assert.AreEqual<string>("provider", ((ArgumentNullException)e).ParamName);
                hasException = true;
            }

            Assert.IsTrue(hasException);
            hasException = false;

            try
            {
                GetEnumeratorTest2Helper<GenericParameterHelper>();
            }
            catch (Exception e)
            {
                Assert.AreEqual<Type>(typeof(ArgumentNullException), e.GetType());
                Assert.AreEqual<string>("expression", ((ArgumentNullException)e).ParamName);
                hasException = true;
            }

            Assert.IsTrue(hasException);
            hasException = false;

            try
            {
                GetEnumeratorTest3Helper<GenericParameterHelper>();
            }
            catch (Exception e)
            {
                Assert.AreEqual<Type>(typeof(ArgumentOutOfRangeException), e.GetType());
                hasException = true;
            }

            Assert.IsTrue(hasException);
        }

        /// <summary>
        ///A test for GetEnumerator
        ///</summary>
        public void GetEnumeratorTestHelper<T>()
        {
            TTableStorageQueryProvider provider = null;
            Expression expression = null;
            TTableStorageQuery<T> target = new TTableStorageQuery<T>(provider, expression);
            IEnumerator<T> actual;
            actual = target.GetEnumerator();
        }

        /// <summary>
        ///A test for System.Collections.IEnumerable.GetEnumerator
        ///</summary>
        public void GetEnumeratorTest2Helper<T>()
        {
            TTableStorageQueryProvider provider = new TTableStorageQueryProvider(
                new TTableStorageInfo(typeof(TTestTableEntityDot).Name),
                (t) =>
                {
                    return new TTableStorageEngine(
                        CloudStorageAccount.DevelopmentStorageAccount.TableEndpoint,
                        CloudStorageAccount.DevelopmentStorageAccount.Credentials,
                        new List<TTableStorageInfo>()
                        {
                            new TTableStorageInfo()
                            {
                                ClassType = typeof(TTestTableEntityDot),
                                TableName = t,
                                PropertyList = new List<TTablePropertyInfo>()
                                {
                                    new TTablePropertyInfo(typeof(TTestTableEntityDot).GetProperty("RowKey"),"RowKey")
                                }
                            }
                        },
                        t,
                        RetryPolicies.Retry(3, new TimeSpan(0, 0, 3)),
                        null
                        );
                }, null
                );

            Expression expression = null;
            IEnumerable target = new TTableStorageQuery<T>(provider, expression);
            IEnumerator actual;
            actual = target.GetEnumerator();
        }

        public void GetEnumeratorTest3Helper<T>()
        {
            TTableStorageQueryProvider provider = new TTableStorageQueryProvider(
                new TTableStorageInfo(typeof(TTestTableEntityDot).Name),
                (t) =>
                {
                    return new TTableStorageEngine(
                        CloudStorageAccount.DevelopmentStorageAccount.TableEndpoint,
                        CloudStorageAccount.DevelopmentStorageAccount.Credentials,
                        new List<TTableStorageInfo>()
                        {
                            new TTableStorageInfo()
                            {
                                ClassType = typeof(TTestTableEntityDot),
                                TableName = t,
                                PropertyList = new List<TTablePropertyInfo>()
                                {
                                    new TTablePropertyInfo(typeof(TTestTableEntityDot).GetProperty("RowKey"),"RowKey")
                                }
                            }
                        },
                        t,
                        RetryPolicies.Retry(3, new TimeSpan(0, 0, 3)),
                        null
                        );
                },
                null
                );

            Expression expression = Expression.Constant(typeof(Expression));
            IEnumerable target = new TTableStorageQuery<T>(provider, expression);
            IEnumerator actual;
            actual = target.GetEnumerator();
        }

        /// <summary>
        /// A test for IQueryable.ElementType
        /// </summary>
        [TestMethod]
        public void ElementTypeTest()
        {
            TTableStorageQuery<object> query = new TTableStorageQuery<object>(new TTableStorageInfo(string.Empty), null, null, null);
            Assert.AreEqual<Type>(typeof(object), query.ElementType);
        }
    }
}
